#include "tensor.h"
#include <fstream>

Tensor Tensor::fromFtmpFile(const std::string& ftmp_file, const std::vector<int>& ftmp_dims) {
	auto size = std::accumulate(ftmp_dims.begin(), ftmp_dims.end(), (int)!ftmp_dims.empty(), std::multiplies<int>());
	if (!std::filesystem::exists(ftmp_file)) {
		std::cout << "FtmpFile does not exist!" << std::endl;
		exit(-1);
	}

	std::ifstream ftmp_stream(ftmp_file, std::ios::binary);

	if (!ftmp_stream) {
		std::cout << "Error : FtmpFile is invalid!" << std::endl;
		system("pause");
		exit(-1);
	}

	std::shared_ptr<int8_t[]> data_int8_t(new int8_t[size]);
	std::shared_ptr<int8_t[]> trans_int8_t(new int8_t[size]);
	ftmp_stream.read((char*)data_int8_t.get(), size * sizeof(int8_t));
	ftmp_stream.close();
	return Tensor(data_int8_t, ftmp_dims);
}


void Tensor::outFile(const std::string out_dir) const{
	std::ofstream output(out_dir, std::ios::binary);

	output.write((char*)(this->data().get()), this->size() * sizeof(int8_t));
	output.close();
	return;
}

// Tensor Tensor::InitInput(const std::vector<int>& ftmp_dims){
// 	//std::shared_ptr<int8_t[]> data(new int8_t[28*28]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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